package com.wwwarehouse.usercenter.fragment.business_relationship;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.CustomViewPagerInternal;
import com.wwwarehouse.common.custom_widget.ElasticScrollView;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tripartite_widget.viewindicator.CirclePageIndicator;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.adapter.SelectPermissionPagerAdapter;
import com.wwwarehouse.usercenter.bean.business_relationship.GetRelationSalesmansBean;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by ShineF on 2017/9/25 0025.
 */
public class BRCFCDSelectSalesmanFragment extends BaseFragment implements CustomViewPagerInternal.OnPageChangeListener {

    private StateLayout mStateLayout;
    private CustomViewPagerInternal mCustomPager;
    private CirclePageIndicator mCirclePager;
    private Button mDistributeBtn;
    private RelativeLayout mRlNoData;
    private static final int SIZE = 9;
    private static final int PAGE = 1;
    private List<Fragment> mFragmentList;
    private ElasticScrollView mCustomScrollView;
    private ArrayList<GetRelationSalesmansBean> mSelectList;
    private ArrayList<GetRelationSalesmansBean> mData;
    private SelectPermissionPagerAdapter mViewPagerAdapter;
    private String mBusinessId;
    private String mRelationUkid;
    private List<GetRelationSalesmansBean> myList;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mRelationUkid = bundle.getString("relationUkid");
            mBusinessId = bundle.getString("buId");
            mData = bundle.getParcelableArrayList("salesManIds");
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_select_distribution_pager_permission, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mStateLayout = $(R.id.state_layout);
        mCustomPager = $(R.id.custom_pager);
        mCirclePager = $(R.id.pager_indicator);
        mDistributeBtn = $(R.id.btn_distribute);
        mCustomScrollView = $(R.id.esv_view);
        mRlNoData = $(R.id.rl_no_data);
        mCustomScrollView.setVisibility(View.INVISIBLE);
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof BRCFCDSelectSalesmanFragment) {
            mActivity.setTitle(getString(R.string.user_br_salesman));
        }
    }

    @Override
    public void requestDatas() {
        mSelectList = new ArrayList<>();
        mDistributeBtn.setVisibility(View.GONE);
        mStateLayout.showProgressView(true);
        mFragmentList = new ArrayList<>();
        if (mData == null) {
            mData = new ArrayList<>();
            requestParams();
        } else {
            setData();
        }
        cardDeskOption();
        mStateLayout.setEmptyListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                requestParams();
            }
        });
        mStateLayout.setSystemListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                requestParams();
            }
        });
    }

    public void requestParams() {
        final Map<String, Object> map = new HashMap<>();
        map.put("buId", mBusinessId);
        map.put("relationUkid", mRelationUkid);
        loadData(UserCenterConstant.GET_RELATION_SALESMANS, map, 0);

    }

    public void loadData(String url, Map map, int what) {
        NoHttpUtils.httpPost(url, map, new NoHttpUtils.OnResponseListener() {
            @Override
            public void onSucceed(CommonClass commonClass, int i) {
                switch (i) {
                    case 0:
                        if ("0".equals(commonClass.getCode())) {
                            if (null != commonClass.getData()) {
                                mRlNoData.setVisibility(View.GONE);
                                mData = (ArrayList<GetRelationSalesmansBean>) JSON.parseArray(commonClass.getData().toString(), GetRelationSalesmansBean.class);
                                if (mData == null || mData.size() == 0) {
                                    mRlNoData.setVisibility(View.VISIBLE);
                                } else {
                                    setData();
                                }
                            } else {
                                mStateLayout.showSystemView(commonClass.getMsg(), false);
                            }
                        }
                        break;
                }


            }

            @Override
            public void onFailed(String s, int i) {

            }

            @Override
            public void onFinish(int i) {
                if (1 == i || 3 == i) {
                    dismissProgressDialog();
                }
            }
        }, what);
    }

    private void cardDeskOption() {
        ((BaseCardDeskActivity) mActivity).hideFilterBt();
        ((BaseCardDeskActivity) mActivity).hideSortBt();
        mCustomScrollView.setOnPullListener(new ElasticScrollView.OnPullListener() {
            @Override
            public void onDownPull() {
                ((BaseCardDeskActivity) mActivity).downPull_4();
            }

            @Override
            public void onUpPull() {

            }
        });

    }

    @Override
    public void searchNoRealTimeClick(final String text) {
        super.searchNoRealTimeClick(text);

        ArrayList<GetRelationSalesmansBean> mSearchData = new ArrayList<>();
        for (GetRelationSalesmansBean getRelationSalesmansBean : myList) {
            if (getRelationSalesmansBean.getUserName().contains(text)) {
                mSearchData.add(getRelationSalesmansBean);
            }
        }
        List<GetRelationSalesmansBean> list = new ArrayList<>();
        mFragmentList.clear();
        // 分页
        if (TextUtils.isEmpty(text)) {
            for (int i = 0; i < mData.size(); i++) {
                list.add(mData.get(i));
                if (list.size() == 9 || i == mData.size() - 1) {
                    ArrayList<GetRelationSalesmansBean> perPageList = new ArrayList<>();
                    perPageList.addAll(list);
                    BRCFCDSelectSalesmanListFragment fragment = new BRCFCDSelectSalesmanListFragment();
                    Bundle bundle = new Bundle();
                    bundle.putParcelableArrayList("perPageList", perPageList);
                    fragment.setArguments(bundle);
                    mFragmentList.add(fragment);
                    list.clear();
                }
            }
        } else {
            for (int i = 0; i < mSearchData.size(); i++) {
                list.add(mSearchData.get(i));
                if (list.size() == 9 || i == mSearchData.size() - 1) {
                    ArrayList<GetRelationSalesmansBean> perPageList = new ArrayList<>();
                    perPageList.addAll(list);
                    BRCFCDSelectSalesmanListFragment fragment = new BRCFCDSelectSalesmanListFragment();
                    Bundle bundle = new Bundle();
                    bundle.putParcelableArrayList("perPageList", perPageList);
                    fragment.setArguments(bundle);
                    mFragmentList.add(fragment);
                    list.clear();
                }
            }
        }
        if (mSearchData.size() == 0){
            mStateLayout.showEmptyView(getString(R.string.user_br_no_data),true);
            mStateLayout.setEmptyListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    searchNoRealTimeClick(text);
                }
            });
        }else{
            mStateLayout.showContentView();
        }

        mViewPagerAdapter = new SelectPermissionPagerAdapter(getChildFragmentManager(), mFragmentList);
        mCustomPager.setAdapter(mViewPagerAdapter);
        mCirclePager.setViewPager(mCustomPager);
        mCirclePager.notifyDataSetChanged();
    }

    @Override
    public void cancelClick() {
        super.cancelClick();
        mStateLayout.showContentView();
        List<GetRelationSalesmansBean> list = new ArrayList<>();
        mFragmentList.clear();
        for (int i = 0; i < mData.size(); i++) {
            list.add(mData.get(i));
            if (list.size() == 9 || i == mData.size() - 1) {
                ArrayList<GetRelationSalesmansBean> perPageList = new ArrayList<>();
                perPageList.addAll(list);
                BRCFCDSelectSalesmanListFragment fragment = new BRCFCDSelectSalesmanListFragment();
                Bundle bundle = new Bundle();
                bundle.putParcelableArrayList("perPageList", perPageList);
                fragment.setArguments(bundle);
                mFragmentList.add(fragment);
                list.clear();
            }
        }
        mViewPagerAdapter = new SelectPermissionPagerAdapter(getChildFragmentManager(), mFragmentList);
        mCustomPager.setAdapter(mViewPagerAdapter);
        mCirclePager.setViewPager(mCustomPager);
        mCirclePager.notifyDataSetChanged();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        ((BaseCardDeskActivity) mActivity).showFilterBt();
        ((BaseCardDeskActivity) mActivity).showSortBt();
    }

    private void setData() {
        mFragmentList.clear();
        if (null != mData) {
            myList = new ArrayList<>();
            for (GetRelationSalesmansBean getRelationSalesmansBean : mData) {
                if (getRelationSalesmansBean.getIsMySalesman().equals("1")) {
                    myList.add(getRelationSalesmansBean);
                }
            }
            List<GetRelationSalesmansBean> list = new ArrayList<>();
            // 分页
            for (int i = 0; i < myList.size(); i++) {
                list.add(myList.get(i));
                if (list.size() == 9 || i == myList.size() - 1) {
                    ArrayList<GetRelationSalesmansBean> perPageList = new ArrayList<>();
                    perPageList.addAll(list);
                    BRCFCDSelectSalesmanListFragment fragment = new BRCFCDSelectSalesmanListFragment();
                    Bundle bundle = new Bundle();
                    bundle.putParcelableArrayList("perPageList", perPageList);
                    fragment.setArguments(bundle);
                    mFragmentList.add(fragment);
                    list.clear();
                }
            }
            if (isAdded()) {
                mViewPagerAdapter = new SelectPermissionPagerAdapter(getChildFragmentManager(), mFragmentList);
                mCustomPager.setAdapter(mViewPagerAdapter);
                mCirclePager.setViewPager(mCustomPager);
                mCustomPager.setOffscreenPageLimit(5);
                mStateLayout.showContentView();
                mCustomScrollView.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

}
